package com.example.schoolexam2.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.schoolexam2.entity.DepartmentTableBean;
import com.example.schoolexam2.entity.PowerVO;
import com.example.schoolexam2.service.PowerService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/power")
public class PowerController {
    @Autowired
    PowerService powerService;

    @RequestMapping("/getModules")
    Map<String,Object> getModules(@RequestBody String obj){
        PowerVO power = formatPageVo(obj);
        power.setUid("admin");
        Map<String,Object> map = new HashMap<>();
        map.put("data",powerService.getPower(power));
        return map;
    }

    @RequestMapping("/getDepartment")
    List<DepartmentTableBean> getModules(){
        return powerService.getDepartInfo();
    }

    @RequestMapping("/getUserTree")
    Map<String,Object> getUserTree(@RequestBody String obj){
        Map<String,Object> map = new HashMap<>();
        map.put("data",powerService.getTearcherBydepartment());
        return map;
    }

    @RequestMapping("/getPageTransfer")
    Map<String,Object> getPageTransfer(@RequestBody String obj){
        Map<String,Object> map = new HashMap<>();
        map.put("data",powerService.getPages());
        return map;
    }

    @RequestMapping("/updatePowerById")
    Map<String,Object> updatePowerById(@RequestBody String obj){
        JSONObject object = JSONObject.parseObject(obj);
        Map<String,Object> map = new HashMap<>();
        powerService.updatePowerById(object);
        map.put("msg","操作成功");
        return map;
    }

    private PowerVO formatPageVo(String obj){
        Gson gson = new Gson();
        PowerVO power = new PowerVO();
        return power = gson.fromJson(obj,PowerVO.class);
    }


}
